// work/pages/add_riCheng/add_riCheng.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schedule: {
      scheduleName: '',
      startDay: '',
      startTime: '',
      endDay: '',
      endTime: '',
      weekName: '',
      memberId: app.globalData.memberId,
      scheduleContent: '',
    },

    startTime: '', //开始时间
    endTime: '', //结束时间
    flag: '', //判断是开始还是结束时间
    timeTitle: '', //开始时间结束时间标题
    showTime: false, //开始结束时间
    currentDate: new Date().getTime(), //选择时间
    value: '',
    formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      if (type === 'hour') {
        return val + '时';
      }
      if (type === 'minute') {
        return val + '分';
      }
      return val;
    },
  },

  addSchedule() {
    var schedule = this.data.schedule;
    console.log(this.data.schedule);

    if (schedule.scheduleName.length == 0) {
      wx.showToast({
        title: '请填写事件标题',
        icon: 'none'
      })
      return
    }
    if (schedule.startTime.length == 0) {
      wx.showToast({
        title: '请选择开始时间',
        icon: 'none'
      })
      return
    }
    if (schedule.endTime.length == 0) {
      wx.showToast({
        title: '请选择结束时间',
        icon: 'none'
      })
      return
    }
    console.log(schedule);
    // return
    var url = app.globalData.url + 'schedule/addSchedule';
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, schedule, res => {
      if (res.flag) {
        console.log(res);
        wx.showModal({
          title: '日程',
          content: '已新增日程安排',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
        wx.hideLoading()
      }
    })
  },



  //判断时间遮罩标题
  timeData(e) {
    var flag = e.currentTarget.dataset.flag,
      timeTitle = '',
      showTime = '';
    if (flag == 1) {
      timeTitle = '开始'
    } else if (flag == 0) {
      timeTitle = '结束'
    }
    showTime = true;
    this.setData({
      flag,
      timeTitle,
      showTime,
    })
  },
  // 选择时间
  resTime(e) {
    var time = new Date(e.detail),
      flag = this.data.flag,
      str = '',
      formartTime = this.formatDate(time);
    console.log(time);
    console.log(formartTime);

    if (flag == 1) {
      str = 'startTime'
    } else if (flag == 0) {
      str = 'endTime'
    }
    this.setData({
      [str]: formartTime,
      showTime: false
    })
  },

  //处理时间格式
  formatDate(date) {
    var schedule = this.data.schedule,
      flag = this.data.flag;
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    if (month < 10) {
      month = '0' + month;
    }
    var days = date.getDate();
    var day = date.getDay();
    console.log(day);
    var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    var week = weeks[day];
    var hour = date.getHours();
    var min = date.getMinutes();
    if (days < 10) {
      days = '0' + days;
    }
    if (hour < 10) {
      hour = '0' + hour;
    }
    if (min < 10) {
      min = '0' + min;
    }
    if (hour <= 12) {
      var text = '上午'
    } else {
      var text = '下午'
    }
    var choose_date = year + '-' + month + '-' + days;
    var time = hour + ':' + min;
    schedule.weekName = schedule.weekName == '' ? week : schedule.weekName;
    console.log(flag);
    if (flag == 1) {
      schedule.startDay = choose_date;
      schedule.startTime = time;
    } else {
      schedule.endDay = choose_date;
      schedule.endTime = time;
    }
    this.setData({
      schedule
    })

    return year + '年' + month + '月' + days + '日' + week + ' ' + text + hour + ':' + min;
  },

  // 取消选择时间 
  selectCancel() {
    this.setData({
      showTime: false
    })
  },
  inputHandle(e) {
    console.log(e);
    var val = e.detail,
      str = e.currentTarget.dataset.str;
    this.setData({
      [str]: val
    })
    console.log(this.data);

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var schedule = this.data.schedule;
    schedule.memberId = app.globalData.memberId
    console.log(schedule);
    this.setData({
      schedule
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})